import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';

// 1、用GestureDetector进行手势检测

//void main(){
//  runApp(new GestureDetectorDemo());
//}

class GestureDetectorDemo extends StatelessWidget{
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return new MaterialApp(
      title: "",
      home: new Scaffold(
        appBar: AppBar(
          title: Text("GestureDetector进行手势检测"),
          centerTitle: true,
        ),
        body: new Center(
          child: new MyButton(),
        ),
      ),
    );
  }

}

class MyButton extends StatelessWidget{
  @override
  Widget build(BuildContext context) {
    // 一定要把被触摸的组件放在 GestureDetector 里面
    return new GestureDetector(
      onTap: (){
        final snackBar = new SnackBar(
            content: Text("你已按下"),
        );
        Scaffold.of(context).showSnackBar(snackBar);
      },
      //添加容器接收触摸动作
      child: new Container(
        padding: EdgeInsets.all(12.0),
        decoration: new BoxDecoration(
          color: Theme.of(context).buttonColor,
          borderRadius: new BorderRadius.circular(10.0),
        ),
        child: Text("测试按钮"),
      ),
    );
  }

}

// 2、用Dismissible实现滑动删除

void main(){
  runApp(new DismissibleDemo());
}

class DismissibleDemo extends StatelessWidget{

  //构造30条列表数据
  List<String> items = new List<String>.generate(30, (i) => "列表项${i + 1}");

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return new MaterialApp(
      title: "",
      home: new Scaffold(
        appBar: AppBar(
          title: Text("Dismissible实现滑动删除"),
          centerTitle: true,
        ),
        //构造列表
        body: new ListView.builder(
            //指定列表长度
            itemCount: items.length,
            //构造列表
            itemBuilder: (context,index){
              //提取出被删除的项
              final item = items[index];
              //返回一个可以被删除的项
              return new Dismissible(
                  key: new Key(item),
                  //被删除的回调
                  onDismissed: (direction){
                    //移除指定索引项
                    items.removeAt(index);
                    //底部提示
                    Scaffold.of(context).showSnackBar(new SnackBar(
                        content: Text("$item 被删除了")
                    ));
                  },
                  child: new ListTile(
                    title: Text("$item"),
                  )
              );
            }
        ),
      ),
    );
  }

}